﻿using DNFImagePacks2Manager.Helpers;
using DNFImagePacks2Manager.NPK;
using Microsoft.UI.Xaml.Controls;
using NPinyin;
using System;
using System.Collections.Generic;
using System.IO;

namespace DNFImagePacks2Manager.Models
{
    public abstract class NPKItemBase : PinYinItem
    {

        public static readonly string DisabledSuffix = ".disable";
        public string BasePath { get; private set; }
        public string Suffix { get; set; }
        public string DisplayPrefix { get; set; }
        public string DisplayName => DisplayPrefix + ": " + Name;
        public string EnableName => Name + Suffix;
        public string DisableName => EnableName + DisabledSuffix;
        public bool Status { get; set; }
        public string EnableFile => Path.Combine(BasePath, EnableName);
        public string DisableFile => Path.Combine(BasePath, DisableName);
        public bool IsFileExist => Path.Exists(EnableFile) || Path.Exists(DisableFile);
        public string FilePath => Status ? EnableFile : DisableFile;

        public NPKItemBase(string prefix, string fileName, string bashPath)
        {
            BasePath = bashPath;
            DisplayPrefix = prefix;

            if (IsOfficalName(fileName))
            {
                fileName = "(Import)" + fileName;
            }

            if (fileName.EndsWith(DisabledSuffix))
            {
                Status = false;
                string enableName = Path.GetFileNameWithoutExtension(fileName);
                Name = Path.GetFileNameWithoutExtension(enableName);
                Suffix = Path.GetExtension(enableName);
            }
            else if (fileName.EndsWith(".npk", StringComparison.OrdinalIgnoreCase))
            {
                Status = true;
                Name = Path.GetFileNameWithoutExtension(fileName);
                Suffix = Path.GetExtension(fileName);
            }
            else
            {
                Status = false;
                Name = fileName;
                Suffix = "";
            }
        }

        public abstract bool IsOfficalName(string name);
        public abstract bool RenameCheck(Page page, string newName);

        // 启用补丁
        public void EnableNPK()
        {
            if (File.Exists(DisableFile))
            {
                try
                {
                    File.Move(DisableFile, EnableFile);
                }
                catch (Exception)
                {

                }
            }
            UpdateStatus();
        }

        // 禁用补丁
        public void DisableNPK()
        {
            if (File.Exists(EnableFile))
            {
                try
                {
                    File.Move(EnableFile, DisableFile);
                }
                catch (Exception)
                {

                }
            }
            UpdateStatus();
        }

        // 删除补丁文件
        public bool DeleteFile()
        {
            try
            {
                if (File.Exists(EnableFile))
                {
                    File.Delete(EnableFile);
                }

                if (File.Exists(DisableFile))
                {
                    File.Delete(DisableFile);
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // 更新启用状态
        public void UpdateStatus()
        {
            Status = File.Exists(EnableFile);
        }

        public bool Rename(Page page, string newName)
        {

            if (!RenameCheck(page, newName))
            {
                return false;
            }

            string basePath = this.BasePath;

            try
            {
                string newEnableName = newName + this.Suffix;
                string newDisableName = newEnableName + DisabledSuffix;
                string newEnablePath = Path.Combine(basePath, newEnableName);
                string newDisablePath = Path.Combine(basePath, newDisableName);
                if (this.Status)
                {
                    if (File.Exists(this.EnableFile))
                    {
                        File.Move(this.EnableFile, newEnablePath);
                    }
                }
                else
                {
                    if (File.Exists(this.DisableFile))
                    {
                        File.Move(this.DisableFile, newDisablePath);
                    }
                }
                this.Name = newName;
                return true;
            }
            catch (Exception)
            {
                DialogHelper.ShowErrorMessage(page, "重命名失败，请确保文件未被其他程序占用");
                return false;
            }
        }

        public void Split(Page page)
        {
            List<NPKIndex> itemList = [];
            string path = FilePath;
            if (File.Exists(path))
            {
                int type = NPKHelper.GetNPKType(path);
                if (type > 0)
                {
                    NPKFile? file = NPKFile.ReadFromNPK(this);
                    if (file != null)
                    {
                        foreach (NPKIndex index in file.npkIndexs.Values)
                        {
                            itemList.Add(index);
                        }
                    }
                }
            }
            switch (itemList.Count)
            {
                case 0:
                    DialogHelper.ShowErrorMessage(page, "文件可能被加密或被占用，无法拆分");
                    break;
                case 1:
                    DialogHelper.ShowErrorMessage(page, "文件只有一个IMG/OGG，无法拆分");
                    break;
                default:
                    App.GetMainWindow()?.ChangeAndInitializePage("ImgSplit", (this, itemList));
                    break;
            }
        }
    }
}
